-- This file is part of the Fantasy Grounds Open Foundation Ruleset project. 
-- For the latest information, see http://www.fantasygrounds.com/
--
-- Copyright 2008 SmiteWorks Ltd.
--
-- This file is provided under the Open Game License version 1.0a
-- Refer to the license.html file for the full license text
--
-- All producers of work derived from this material are advised to
-- familiarize themselves with the license, and to take special
-- care in providing the definition of Product Identity (as specified
-- by the OGL) in their products.
--
-- All material submitted to the Open Foundation Ruleset project must
-- contain this notice in a manner applicable to the source file type.


-- Token properties

function getTokenUnderlayProperties()
	local s = math.ceil(space.getValue() / 5) / 2;
	local r = math.ceil(reach.getValue() / 5) + s;
	
	-- space, reach, gmonly
	return s, r, true;
end

function getTokenPermissions()
	-- visibility, modifiability
	return nil, false;
end

function handleTokenDrop(token, draginfo)
	if draginfo.isType("number") then
		print("drop");
		wounds.setValue(wounds.getValue() + draginfo.getNumberData());
		return true;
	end
end


-- Section visibility handling

forceactive = false;
forcedefensive = false;
defensiveon = false;
activeon = false;


function setSpacerState()
	if defensiveon or activeon or effectson then
		spacer.setVisible(true);
	else
		spacer.setVisible(false);
	end
end

function setDefensiveVisible(status)
	if forcedefensive then
		status = true;
	end
	if not targeting.isEmpty() then
		status = true;
	end
	
	defensiveon = status;

	ac.setVisible(status);
	aclabel.setVisible(status);
	fortitudesave.setVisible(status);
	fortitudesavelabel.setVisible(status);
	reflexsave.setVisible(status);
	reflexsavelabel.setVisible(status);
	willsave.setVisible(status);
	willsavelabel.setVisible(status);
	targeting.setVisible(status);
	defensiveicon.setVisible(status);
	
	setSpacerState();
end
	
function setActiveVisible(status)
	if forceactive then
		status = true;
	end
	if active.getState() then
		status = true;
	end
	
	activeon = status;

	speed.setVisible(status);
	speedlabel.setVisible(status);
	init.setVisible(status);
	initlabel.setVisible(status);
	atk.setVisible(status);
	atklabel.setVisible(status);
	fullatk.setVisible(status);
	fullatklabel.setVisible(status);
	activeicon.setVisible(status);
	
	setSpacerState();
end

function toggleForceActive()
	forceactive = not forceactive;
	
	if forceactive then
		activateactive.setColor("ffffffff");
	else
		activateactive.setColor("7fffffff");
	end
end

function toggleForceDefensive()
	forcedefensive = not forcedefensive;
	
	if forcedefensive then
		activatedefensive.setColor("ffffffff");
	else
		activatedefensive.setColor("7fffffff");
	end
end


function onInit()
	super.onInit();

	setDefensiveVisible(false);
	setActiveVisible(false);
end


function populate(itemdatanode, combatdatanode)
	if combatdatanode then
		name.setValue(combatdatanode.getChild("name").getValue());
	else
		name.setValue(itemdatanode.getChild("name").getValue());
	end
	link.setValue("npc", itemdatanode.getNodeName());
end
